package sh.wn36.vehiclemanage.controller.cjl_controller;


import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sh.wn36.vehiclemanage.entity.entity_cjl.Maintain;
import sh.wn36.vehiclemanage.entity.entity_cjl.reminder.Reminder;
import sh.wn36.vehiclemanage.entity.entity_cjl.reminder.Reminder_InspectionRecord;
import sh.wn36.vehiclemanage.entity.entity_cjl.reminder.Reminder_insuranceRecordService;
import sh.wn36.vehiclemanage.service.CarService;
import sh.wn36.vehiclemanage.service.InspectionRecordService;
import sh.wn36.vehiclemanage.service.InsuranceRecordService;
import sh.wn36.vehiclemanage.service.service_cjl.MaintainRecordService;
import sh.wn36.vehiclemanage.util.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/reminderController")
@Api("到期提醒模块接口")
public class reminderController {

    @Autowired
    private CarService carService;

    //保养记录
    @Autowired
    private MaintainRecordService maintainRecordService;

    //年检记录
    @Autowired
    private InspectionRecordService inspectionRecordService;

    //保险记录
    @Autowired
    private InsuranceRecordService insuranceRecordService;

    @GetMapping("/reminder/{current}/{limit}")
    public Result reminder(@PathVariable("current") Integer current, @PathVariable("limit") Integer limit) throws ParseException {

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<Reminder> reminderArrayList = new ArrayList<>();

        List<Maintain> maintainRecords = maintainRecordService.listMaintainRecord();
        for (Maintain maintainRecord : maintainRecords) {
            String nowTime = format.format(date);
            String nextTime = format.format(maintainRecord.getNextDate());
            long time = (format.parse(nextTime).getTime() - format.parse(nowTime).getTime()) / (60000 * 60 * 24);
            if (time < 7 && time > 0) {
                Reminder reminder = new Reminder("保养到期提醒");
                reminder.setCarNo(maintainRecord.getCarNo());
                reminder.setNextdate(maintainRecord.getNextDate());
                reminderArrayList.add(reminder);
            }
        }

        List<Reminder_InspectionRecord> reminder_inspectionRecords = inspectionRecordService.listReminder_InspectionRecord();
        for (Reminder_InspectionRecord reminder_inspectionRecord : reminder_inspectionRecords) {
            String nowTime = format.format(date);
            String nextTime = format.format(reminder_inspectionRecord.getExpireDate());
            long time = (format.parse(nextTime).getTime() - format.parse(nowTime).getTime()) / (60000 * 60 * 24);
            if (time < 7 && time > 0) {
                Reminder reminder = new Reminder("车检到期提醒");
                reminder.setCarNo(reminder_inspectionRecord.getCarNo());
                reminder.setNextdate(reminder_inspectionRecord.getExpireDate());
                reminderArrayList.add(reminder);
            }
        }

        List<Reminder_insuranceRecordService> reminder_insuranceRecordServices = insuranceRecordService.listReminder_insuranceRecordService();
        for (Reminder_insuranceRecordService reminder_insuranceRecordService : reminder_insuranceRecordServices) {
            String nowTime = format.format(date);
            String nextTime = format.format(reminder_insuranceRecordService.getExpireDate());
            long time = (format.parse(nextTime).getTime() - format.parse(nowTime).getTime()) / (60000 * 60 * 24);
            if (time < 7 && time > 0) {
                Reminder reminder = new Reminder("保险到期提醒");
                reminder.setCarNo(reminder_insuranceRecordService.getCarNo());
                reminder.setNextdate(reminder_insuranceRecordService.getExpireDate());
                reminderArrayList.add(reminder);
            }
        }
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!"+reminderArrayList);
        List<Reminder> pageList = getPageList(reminderArrayList, current, limit);
        return Result.OK().data("pageList", pageList).data("total",reminderArrayList.size());
    }


    public List<Reminder> getPageList(List<Reminder> listPage, Integer pageNum, Integer pageSize) {
        if (listPage == null) {
            return null;
        }
        if (listPage.size() == 0) {
            return null;
        }
        Integer count = listPage.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        List pageList = listPage.subList(fromIndex, toIndex);
        return pageList;
    }
}
